<?php defined('SYSPATH') or die('No direct script access.'); ?>
{php echo Common::getCss('base.css,base_new.css'); }
{template 'stourtravel/public/public_min_js'}
{php echo Common::getScript("uploadify/jquery.uploadify.min.js,DatePicker/WdatePicker.js,choose.js,product_add.js,jquery.validate.js"); }
{php echo Common::getCss('uploadify.css','js/uploadify/'); }

<div class="container-page">
    <form id="submit_frm">
        <ul class="info-item-block">
            {if !$set_date}
            <li>
                <span class="item-hd" style="width: 120px">日期范围：</span>
                <div class="item-bd">
                    <input type="text" name="start_time" class="input-text w150 choosetime" value="{$min_day}"/>
                    <span class="item-text pl-5 pr-5 c-999">—</span>
                    <input type="text" name="end_time" class="input-text w150 choosetime" value="{$max_day}"/>
                </div>
            </li>
            <li>
                <span class="item-hd">报价日期：</span>
                <div class="item-bd">
                    <div class="date-scope-container">
                        <div class="ds-tab-nav">
                            <label class="radio-label"><input type="radio" name="price_rule" value="1" checked/>全部日期范围</label>
                            <label class="radio-label ml-50"><input type="radio" name="price_rule" value="2"/>日期范围下的星期</label>
                            <label class="radio-label ml-50"><input type="radio" name="price_rule" value="3"/>日期范围下的天</label>
                        </div>
                        <div class="ds-tab-box mt-10 clearfix tab-box-3" style="display: none">
                            <?php for ($i = 1; $i <= 31; $i++) {
                                echo " <span class='item' data-id='$i'>$i</span>";
                            }
                            ?>
                        </div>
                        <div class="ds-tab-box mt-10 clearfix tab-box-2" style="display: none">
                            <span class="item" data-id="1">星期一</span>
                            <span class="item" data-id="2">星期二</span>
                            <span class="item" data-id="3">星期三</span>
                            <span class="item" data-id="4">星期四</span>
                            <span class="item" data-id="5">星期五</span>
                            <span class="item" data-id="6">星期六</span>
                            <span class="item" data-id="7">星期天</span>
                        </div>
                    </div>
                </div>
            </li>
            {/if}
            <li>
                <span class="item-hd">价格：</span>
                <div class="item-bd">
                    <table class="table table-border table-bordered table-bg table-order w500">
                        <thead>
                            <tr class="text-c">
                                <th><span class="c-999">人群</span></th>
                                {if $settlement_info['settlement_type']==1}
                                <th><span class="c-999">成本</span></th>
                                <th><span class="c-999">利润</span></th>
                                <th><span class="c-999">售价</span></th>
                                {else}
                                <th><span class="c-999">销售价</span></th>
                                {/if}
                            </tr>
                        </thead>
                        <tbody>
                        {loop $people_group $peo}
                            <tr class="text-c">
                                <td>
                                    {if $peo['identifier']}
                                    <label class="check-label w200" style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis">
                                        <input type="checkbox" {if in_array($peo['identifier'],$price['propgroup'])}checked="checked"{/if} name="propgroup[]" value="{$peo['identifier']}">{$peo['name']}
                                    </label>
                                    {else}
                                    {$peo['name']}
                                    {/if}
                                </td>
                                <td>
                                    <input type="text" class="input-text w100 price-blur" name="{$peo['basicprice_field']}" value="{if $price[$peo['basicprice_field']]>=0}{$price[$peo['basicprice_field']]}{/if}" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"/>
                                </td>
                                {if $settlement_info['settlement_type']==1}
                                <td>
                                    <input type="text" class="input-text w100 price-blur" name="{$peo['profit_field']}" value="{if $price[$peo['profit_field']]>=0}{$price[$peo['profit_field']]}{/if}" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"/>
                                </td>
                                <td>
                                    <span class="price-result">{if $price[$peo['price_field']]>0}{Currency_Tool::symbol()}{$price[$peo['price_field']]}{/if}</span>
                                </td>
                                {/if}
                            </tr>
                        {/loop}
                        </tbody>
                    </table>
                </div>
            </li>
            {if $settlement_info['settlement_type']==2}
            <li>
                <span class="item-hd">抽佣方式：</span>
                <div class="item-bd">
                    <table class="table table-border table-bordered table-bg table-order w500">
                        <thead>
                        <tr class="text-c">
                            <th><span class="c-999">抽佣方式</span></th>
                            <th><span class="c-999">抽佣百分比/固定金额</span></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="text-c">
                            <td>
                                <label class="radio-label mr-10">
                                    <input type="radio" {if $settlement_info['settlement_key']!=2}checked="checked"{/if} name="settlement_key" value="1">百分比
                                </label>
                                <label class="radio-label">
                                    <input type="radio" {if $settlement_info['settlement_key']==2}checked="checked"{/if} name="settlement_key" value="2">固定金额
                                </label>
                            </td>
                            <td>
                                <input type="text" class="input-text w80" value="{if $settlement_info['settlement_val']>0}{$settlement_info['settlement_val']}{/if}" name="settlement_val">
                                <span class="price-unit">{if $settlement_info['settlement_key']!=2}%{else}元{/if}</span>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <script>
                    $(function(){
                        $("[name='settlement_key']").on('click',function(){
                            var key = $(this).val();
                            var val = $("[name='settlement_val']").val();
                            var obj = $(".price-unit");
                            if(key==1){
                                val = val.replace(/\D/g,'');
                                if(val<0 || val>100){
                                    val = val>100?100:0;
                                }
                                $("[name='settlement_val']").val(val);
                                obj.html('%');
                            }else{
                                val = val.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')
                                $("[name='settlement_val']").val(val);
                                obj.html('元');
                            }
                        });
                        $("[name='settlement_val']").keyup(function(){
                            var key = $("[name='settlement_key']:checked").val();
                            var val = $(this).val();
                            if(key==1){
                                val = val.replace(/\D/g,'');
                                if(val>100){
                                    val = 100;
                                }
                                if(val<0){
                                    val = 0;
                                }
                            }else{
                                val = val.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1');
                            }
                            $(this).val(val);
                        });
                    })
                </script>
            </li>
            {/if}
            {if $type_id==1}
            <li>
                <span class="item-hd">单房差：</span>
                <div class="item-bd">
                    <span class="item-text" style="height: 31px">
                        <input type="text" name="roombalance" value="{$price['roombalance']}" class="reset-input input-text w100 va-t" onkeyup="value=value.replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1')"/>
                    </span>
                </div>
            </li>
            {/if}
            <li>
                <span class="item-hd">限购库存：</span>
                <div class="item-bd">
                    <label class="radio-label mr-10">
                        <input type="radio" name="number_type" {if $price['number']=='-1'}checked="checked"{/if} value="1">不限
                    </label>
                    <label class="radio-label">
                        <input type="radio" name="number_type" {if $price['number']!='-1'}checked="checked"{/if} value="2">数量
                        <input type="text" name="number" {if $price['number']=='-1'}style="display:none"{/if} value="{$price['number']}" class="reset-input input-text w100 va-t" onkeyup="this.value=this.value.replace(/\D/g,'')"/>
                    </label>
                </div>
            </li>
        </ul>
        <div id="hidevalue" class="hide">

        </div>
        <input type="hidden" name="suit_id" value="{$suit_id}">
        <input type="hidden" name="type_id" value="{$type_id}">
        <input type="hidden" name="channel_id" value="{$channel_id}">
        <input type="hidden" name="set_date" value="{$set_date}">
        <input type="hidden" name="settlement_type" value="{$settlement_info['settlement_type']}">
    </form>
</div>
<div class="clearfix text-c mt-20">
    <a href="javascript:;" id="cancel-btn" class="btn btn-grey-outline  radius">取消</a>
    <a href="javascript:;" class="btn btn-primary radius ml-10">确定</a>
    {if $set_date}
    <a href="javascript:;" class="btn btn-warning radius ml-10">清除报价</a>
    {/if}
</div>
<script>
    var status = "{$status}";
    var min_day = "{$min_day}";
    var max_day = "{$max_day}";
    var set_date = "{$set_date}";
    var icon = "{Currency_Tool::symbol()}";
    var settlement_type = "{$settlement_info['settlement_type']}";
    if(!status){
        ST.Util.responseDialog({msg: '请设置库存'}, false);
    }
    $(function () {
        $("[name='number_type']").on('click',function(){
            var type = $(this).val();
            if(type==1){
                $("[name='number_type']").siblings('.reset-input').hide();
            }else{
                $("[name='number_type']").siblings('.reset-input').show();
            }
        });
        $('.price-blur').on('blur',function(){
            var price_1 = $(this).val();
            if(settlement_type==1){
                var price_2 = $(this).parent().siblings('td').find('.price-blur').val();
                var total = ST.Math.add(price_1,price_2);
                $(this).parent().siblings('td').find('.price-result').html(icon+total);
                if(icon+total!=icon+'0'){
                    $(this).parent().siblings('td').find("[name='propgroup[]']").attr('checked','checked')
                }else{
                    $(this).parent().siblings('td').find("[name='propgroup[]']").removeAttr('checked')
                }
            }else{
                if(price_1>0){
                    $(this).parent().siblings('td').find("[name='propgroup[]']").attr('checked','checked')
                }else{
                    $(this).parent().siblings('td').find("[name='propgroup[]']").removeAttr('checked')
                }
            }
        });

        //日历选择
        $(".choosetime").click(function () {
            WdatePicker({skin: 'whyGreen', dateFmt: 'yyyy-MM-dd', minDate: min_day, maxDate: max_day})
        });


        //报价方式切换
        $('input[name=price_rule]').change(function () {
            var type = $(this).val();
            $('.tab-box-3').hide();
            $('.tab-box-2').hide();
            if (type == 2) {
                $('.tab-box-2').show();
            }
            if (type == 3) {
                $('.tab-box-3').show();
            }

            //ST.Util.resizeDialog('.container-page');
        });
        //星期选择
        $(".tab-box-2 span").click(function (e) {
            var v = $(this).data('id');
            if ($(this).hasClass('on')) {
                $("#week_val_" + v).remove();
            } else {
                $("#hidevalue").append("<input type='hidden' id='week_val_" + v + "' name='week_val[]' value='" + v + "'/>");
            }
            $(this).toggleClass('on');

        });
        //具体到天
        $(".tab-box-3 span").click(function (e) {

            var v = $(this).data('id');
            v = $.trim(v);
            v = window.parseInt(v);
            if ($(this).hasClass('on')) {
                $("#month_val_" + v).remove();
            } else {
                $("#hidevalue").append("<input type='hidden' id='month_val_" + v + "' name='month_val[]' value='" + v + "'/>");
            }
            $(this).toggleClass('on');
        });

        //重置表单
        $('#clear-data').click(function () {
            ST.Util.confirmBox('提示', '确定要清空报价?', function () {
                $('.reset-box').attr('checked', false);
                $('.reset-input').val('');
                $('.reset-span').text('');
            })
        });

        //清除当天报价
        $('.btn-warning').click(function () {
            var data = $('#submit_frm').serialize();
            $.ajax({
                data: data,
                dataType: 'json',
                type: 'post',
                url: '/newtravel/price/ajax_rm_oneday_price',
                success: function (data) {
                    console.log(data)
                    if(data.status){
                        ST.Util.showMsg(data.msg,4,1000);
                        setTimeout(function(){
                            ST.Util.responseDialog(data, true);
                        },1000)
                    }else{
                        ST.Util.showMsg(data.msg,5,2000);
                    }
                }
            })
        })


        //确定
        $('.btn-primary').click(function () {
            $('.price-result').each(function(){
                var price = $(this).text();
                if(price==icon+'0' || price==''){
                    $(this).parent().siblings('td').find("[name='propgroup[]']").removeAttr('checked')
                }
            });

            var data = $('#submit_frm').serialize();
            var url = set_date?'/newtravel/price/ajax_save_single_price':'/newtravel/price/ajax_save_all_price';
            $.ajax({
                data: data,
                dataType: 'json',
                type: 'post',
                url: url,
                success: function (data) {
                    if(data.status){
                        ST.Util.showMsg(data.msg,4,1000);
                        setTimeout(function(){
                            ST.Util.responseDialog(data, true);
                        },1000)
                    }else{
                        ST.Util.showMsg(data.msg,5,2000);
                    }
                }
            })
        });
        //取消
        $('#cancel-btn').click(function () {
            var data = $('#submit_frm').serialize();
            ST.Util.responseDialog({data: data}, false);
        })
    })
</script>